﻿'***************************************************************************\
' Module Name: CommandLineParser.vb
' Project: SetDefaultAudioEndpoint http://sdae.codeplex.com/
' Copyright 2011 by jeff
' 
' This source is subject to the GNU General Public License version 2 (GPLv2).
' See http://www.gnu.org/licenses/gpl-2.0.html.
' All other rights reserved.
' 
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'***************************************************************************/

Class CommandLineParser

  Enum CommandLineParserResult
    Unparsed = 0
    OK
    Usage
    Fail
  End Enum

  Private myResult As CommandLineParserResult = CommandLineParserResult.Unparsed
  Private myParseError As String

  'Public Function Parse(ByVal args As String(), ByVal universe As ExecutionOptionsList) As ExecutionOptionsList
  Public Sub Parse(ByVal args As String(), ByVal universe As ExecutionOptionsList)
    If (args.Length = 0) Then
      Me.myResult = CommandLineParserResult.Usage
      Return 'nothing
    End If

    Dim parseOk As Boolean = True
    'Dim result As New ExecutionOptionsList

    For Each item As String In args
      If (item.StartsWith("-", StringComparison.OrdinalIgnoreCase) AndAlso (item.Length >= 2) AndAlso parseOk) Then
        Try
          'result.Add(universe.First(Function(anOption) anOption.TestAndSet(item)))
          universe.First(Function(anOption) anOption.TestSwitch(item)).SetSwitch(item)
        Catch ex As InvalidOperationException
          Dim message As String = String.Format(Globalization.CultureInfo.CurrentCulture, "The audio endpoint with name '{0}' could not be found.", name)
          Throw New ParseException(message, ex)
        End Try

        Select Case item.Substring(1, 1).ToUpper(Globalization.CultureInfo.InvariantCulture)
          Case "P"
            Me.myPlaybackDeviceName = item.Substring(2)
          Case "R"
            Me.myRecordingDeviceName = item.Substring(2)
          Case "?", "H"
            Me.myResult = CommandLineParserResult.Usage
          Case Else
            parseOk = False
            Me.myParseError = item
            Me.myResult = CommandLineParserResult.Fail
        End Select
      Else
        parseOk = False
        Me.myParseError = item
        Me.myResult = CommandLineParserResult.Fail
      End If
    Next item

    If ((Me.myResult <> CommandLineParserResult.Fail) _
          AndAlso (Me.myResult <> CommandLineParserResult.Usage) _
          AndAlso parseOk) Then
      Me.myResult = CommandLineParserResult.OK
    End If

    Return 'result
  End Sub

  Public ReadOnly Property Result() As CommandLineParserResult
    Get
      Return Me.myResult
    End Get
  End Property

  Public Shared ReadOnly Property Usage() As String
    Get
      Return My.Resources.ConsoleApplicationUsageText
    End Get
  End Property

  Public ReadOnly Property ParseError() As String
    Get
      Return String.Format(Globalization.CultureInfo.CurrentCulture, "The command line option '{0}' is invalid.", Me.myParseError) _
       & Environment.NewLine & Environment.NewLine & CommandLineParser.Usage
    End Get
  End Property

End Class
